package net.example.model.common;


import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;


import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

/**
 * Created by ZN on 2017/2/8.
 */

public class NonText {
    private int chatBoxElementType;
    private String filePath;
    private byte[] image;
    private boolean gif;
    private long innerEmotionIndex;
    private LinkText linkText;

    public LinkText getLinkText() {
        return linkText;
    }

    public void setLinkText(LinkText linkText) {
        this.linkText = linkText;
    }

    public int getChatBoxElementType() {
        return chatBoxElementType;
    }

    public void setChatBoxElementType(int chatBoxElementType) {
        this.chatBoxElementType = chatBoxElementType;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public boolean isGif() {
        return gif;
    }

    public void setGif(boolean gif) {
        this.gif = gif;
    }

    public long getInnerEmotionIndex() {
        return innerEmotionIndex;
    }

    public void setInnerEmotionIndex(long innerEmotionIndex) {
        this.innerEmotionIndex = innerEmotionIndex;
    }

    public void deserialize(ByteBuf buffer) throws Exception {
        buffer.readInt();
        chatBoxElementType = buffer.readInt();
        filePath = SerializeUtils.readStrIntLen(buffer);
        int imgLen = buffer.readInt();
        if (imgLen > 0) {
            gif = buffer.readBoolean();
            image = new byte[imgLen];
            buffer.readBytes(image);
        }
        innerEmotionIndex = buffer.readUnsignedInt();
        linkText = new LinkText();
        linkText.deserialize(buffer);
    }

    public byte[] serialize() throws Exception {
        ByteBuf body = BufferUtils.newBuffer();
        int totalLen = 0;
        body.writeInt(totalLen);
        body.writeInt(chatBoxElementType);
        totalLen+=4;
        if (filePath != null) {
            byte[] fileBytes = this.filePath.getBytes(StandardCharsets.UTF_8);
            body.writeInt(fileBytes.length);
            body.writeBytes(fileBytes);
            totalLen += 4 +  fileBytes.length;
        }
        else {
            body.writeInt(-1);
            totalLen += 4;
        }
        if (image != null) {
            body.writeInt(image.length);
            body.writeBoolean(gif);
            body.writeBytes(image);
            totalLen += 4 + 1 + image.length;
        } else {
            body.writeInt(-1);
            totalLen += 4;
        }
        body.writeInt((int) innerEmotionIndex);
        totalLen+=4;
        if (linkText != null) {
            byte[] linkBytes = linkText.serialize();
            body.writeInt(linkBytes.length);
            body.writeBytes(linkBytes);
            totalLen += 4 + linkBytes.length;
        } else {
            body.writeInt(-1);
            totalLen += 4;
        }
        body.resetWriterIndex();
        body.writeInt(totalLen);
        byte[] bytes = new byte[4+totalLen];
        body.getBytes(0, bytes);
        return bytes;
    }
}